package edu.zju.gis.dbfg.model.tile;

import edu.zju.gis.dbfg.common.exception.ValidationException;
import lombok.Getter;
import lombok.Setter;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author Hu
 * @date 2019/9/4
 * VectorTileClipper模型的运行参数定义类
 **/
@Getter
@Setter
public class VectorTileClipperArgs implements Serializable {

  private final static Logger logger = LoggerFactory.getLogger(VectorTileClipperArgs.class);

  @Option(name = "-input",usage = "输入数据", required = true)
  private String input;

  @Option(name = "-outdir",usage = "瓦片数据存放路径", required = true)
  private String outDir;

  @Option(name = "-layername",usage = "图层名称", required = true)
  private String layerName;

  @Option(name = "-extent",usage = "图层空间范围, 格式为: xmin, xmax, ymin, ymax",required = true)
  private String extent;

  @Option(name = "-tcrs",usage = "瓦片数据坐标系")
  private String targetCRS = "EPSG:4490";

  @Option(name = "-zmin",usage = "瓦片金字塔范围最小值")
  private int zMin = 4;

  @Option(name = "-zmax",usage = "瓦片金字塔范围最大值")
  private int zMax = 10;

  public  static VectorTileClipperArgs initArgs(String args[]){
    VectorTileClipperArgs mArgs = new VectorTileClipperArgs();
    CmdLineParser parser = new CmdLineParser(mArgs);
    try {
      parser.parseArgument(args);
      if (!mArgs.checkValid()){
        throw new ValidationException(VectorTileClipperArgs.class,"initArgs()", "Unvalid arguments", args);
      }
      return mArgs;
    } catch (CmdLineException e) {
      logger.error(e.getLocalizedMessage());
      return null;
    }
  }

  private boolean checkValid() {
    boolean flag = true;
    flag = flag && (zMin >= 0);
    flag = flag && (zMax <= 20);
    flag = flag && (zMin <= zMax);
    return flag;
  }

}
